package com.twodudesdev.amerikick.learnkarate;

import java.util.List;

import com.twodudesdev.amerikick.learnkarate.DBAdapter;
import com.twodudesdev.amerikick.learnkarate.R;
import com.twodudesdev.amerikick.learnkarate.DBAdapter.DatabaseHelper;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.database.Cursor;
import android.graphics.Typeface;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class AddressActivity extends Activity {

	static final String ACTION = "com.twodudesdev.amerikick.learnkarate.ADDRESS";
	
	static final String CITY = "com.twodudesdev.amerikick.learnkarate.city";
	
	private Cursor addressCursor = null;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.address_info);
		
		setupCursor();
		
		displayAddress();
	}
	
	/**
     * Starts the ShowSchool for the specified location.
     *
     * @param context The application's environment.
     * @param user The city to display with the ShowSchool.
     */
    static void show(Context context, String city) {
        final Intent intent = new Intent(ACTION);
        intent.putExtra(CITY, city);
        context.startActivity(intent);
    }
    
    private void setupCursor() {
    	//Integer intSchool = bdlList.getInt("school");
    	String strCity = getCity();
    	
    	DBAdapter dbLearnKarate = (new DBAdapter(this, DatabaseHelper.DB_NAME, null, DatabaseHelper.DATABASE_VERSION));
    	dbLearnKarate.open();
    	addressCursor = dbLearnKarate.getSchool(strCity);
    	startManagingCursor(addressCursor);
    }
    
    private void displayAddress() {
    	addressCursor.moveToFirst();
        while (addressCursor.isAfterLast() == false) {
            
        	addTableRow("",addressCursor.getString(7));			// Address
	    	addTableRow("Address: ",addressCursor.getString(1));			// Address
	    	addTableRow("",addressCursor.getString(2));					// City State Zip
	    	addTableRow("Phone: ",addressCursor.getString(3));			// Phone
	    	addTableRow("Fax: ",addressCursor.getString(4));				// Fax
	    	addTableRow("Email: ",addressCursor.getString(5));			// Email
	    	addTableRow("Website: ",addressCursor.getString(6));			// Website
	    	
	    	String strAdrUri = addressCursor.getString(1)+" "+addressCursor.getString(2);
	    	strAdrUri = strAdrUri.replace(" ","+");

	    	addTableRowBtn("Map It!", strAdrUri);
	    	//addTableRowCbx("My School",schoolCursor.getInt(8));
	    	
	    	addressCursor.moveToNext();
	    	if (addressCursor.isAfterLast() == false)
	    	{
	    		addTableRow("","");
	    	}
        }
    }
    
    public void addTableRowBtn(String title, final String data)
	{
		//Get a reference to the TableLayout
    	TableLayout table = (TableLayout) findViewById(R.id.school_info_table);
    	   
        // create a new TableRow
        TableRow row = new TableRow(this);
 
        // create a new title TextView
        TextView tvTitle = new TextView(this);
        // set the text to "text xx"
        tvTitle.setText("");
        tvTitle.setTextSize(17);
        
        // create a new Button
        Button btnTitle = new Button(this);
        // set the text to "text xx"
        btnTitle.setText(title);
        btnTitle.setTypeface(Typeface.SERIF);
        btnTitle.setOnClickListener(new View.OnClickListener() {
        	@Override
			public void onClick(View v) {
        		/*
        		Toast.makeText(getApplicationContext(), data,
        	    		Toast.LENGTH_SHORT).show();
        		//*/
        		Intent i = new Intent(Intent.ACTION_VIEW,Uri.parse("geo:0,0?q=" + data));
        		List<ResolveInfo> list = getPackageManager().queryIntentActivities(i, PackageManager.MATCH_DEFAULT_ONLY);
        		if (list.size() == 0)
        		{
        			i = (new Intent(Intent.ACTION_VIEW, Uri.parse("http://maps.google.com/maps?q=" + data)));
        		}
        		startActivity(i);
        	}
        });
        // add the TextView and the CheckBox to the new TableRow
        row.addView(tvTitle);
        row.addView(btnTitle);
 
        // add the TableRow to the TableLayout
        table.addView(row,new TableLayout.LayoutParams(android.view.ViewGroup.LayoutParams.WRAP_CONTENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT));
	}
    
    /**
     * Creates a Sting from the intent used to start this activity.
     *
     * @return The city whose school(s) are to be displayed
     */
    private String getCity() {
        final Intent intent = getIntent();
        final String action = intent.getAction();

        String city = null;

        if (ACTION.equals(action)) {
            final Bundle extras = intent.getExtras();
            if (extras != null) {
            	city = extras.getString(CITY);

                if (city == null) {
                    //location = "State";
                }
            }
        } /*else if (Intent.ACTION_VIEW.equals(action)) {
            final List<String> segments = intent.getData().getPathSegments();
            if (segments.size() > 1) {
                mUsername = segments.get(1);
            }
        }*/

        return city;
    }
    
    public void addTableRow(String title, String data)
	{
		//Get a reference to the TableLayout
    	TableLayout table = (TableLayout) findViewById(R.id.school_info_table);
    	   
        // create a new TableRow
        TableRow row = new TableRow(this);
 
        // create a new title TextView
        TextView tvTitle = new TextView(this);
        // set the text to "text xx"
        tvTitle.setText(title);
        tvTitle.setTextSize(17);
        tvTitle.setTypeface(Typeface.SERIF);
       
               
        // create a new title TextView
        TextView tvData = new TextView(this);
        // set the text to "text xx"
        tvData.setText(data);
        tvData.setTextSize(17);
        tvData.setTypeface(Typeface.SERIF);
        
        tvData.setEms(5);
        tvData.setEllipsize(TextUtils.TruncateAt.END);
        
        // add the TextView and the CheckBox to the new TableRow
        row.addView(tvTitle);
        row.addView(tvData);
 
        // add the TableRow to the TableLayout
        table.addView(row,new TableLayout.LayoutParams(android.view.ViewGroup.LayoutParams.WRAP_CONTENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT));
	}
}
